# -*- coding: utf-8 -*-
"""
Created on Sat Mar  6 11:13:40 2021

@author: wxzx
"""

import os  
import os.path
import pandas as pd
from xml.dom.minidom import parse
import xml.dom.minidom

import time



start =time.clock()

excelFile = r'/MROjizhan.xlsx'
df_jizhan = pd.DataFrame(pd.read_excel(excelFile,sheetname=0))


### import xml.dom.minidom  
path='/mro/'
files=os.listdir(path) #得到文件夹下所有文件名称
#writer = pd.ExcelWriter('F:\MR xml\shuchu.xlsx')
colum = ["eNBID","cellid","值"]
df = pd.DataFrame(index=None,columns=colum)
df1=pd.DataFrame(index=None,columns=colum)
k=0
for xmlFile in files:
    if not os.path.isdir(xmlFile): #判断是否是文件夹，不是文件夹才打开
        print(xmlFile)
        temp=int(xmlFile.split('_')[3])
        if df_jizhan[(df_jizhan['基站']==temp)].index.tolist():
            DOMTree = xml.dom.minidom.parse(os.path.join(path,xmlFile))
            collection = DOMTree.documentElement
            if collection.getElementsByTagName("eNB"):
                en=collection.getElementsByTagName('eNB')[0]
                eb=en.getAttribute("id")
                j = collection.getElementsByTagName("object")  #object节点数目
                for i in j:
                    cell=i.getAttribute("id")
                    name=i.getElementsByTagName('v')
                    for names in name:
                        name_text_node=names.childNodes[0]
                        ss=name_text_node.data
                        ss = ss.split(" ", 26)
                        if k>=2000:
                            k=0
                            df=pd.concat([df, df1],axis=0)
                            df1=pd.DataFrame(index=None,columns=colum)
                        if len(ss)==26:
                            if (ss[0]!="NIL")&(ss[23]!="NIL")&(ss[24]!="NIL"):
                                df1.loc[k,"eNBID"]=eb
                                df1.loc[k,"cellid"]=cell
                                df1.loc[k,"值"]=name_text_node.data
                                k+=1
                            else:
                                continue
                        else:
                            continue
            else:
                continue
        else:
            continue



df=pd.concat([df, df1],axis=0)
df = df.reset_index(drop=True)
df1=pd.DataFrame((x.split(' ') for x in df['值']),index=df.index,columns=['MR.LteScRSRP','MR.LteNcRSRP','MR.LteScRSRQ','MR.LteNcRSRQ','MR.LteScEarfcn','MR.LteScPci','MR.LteNcEarfcn','MR.LteNcPci','MR.LteScTadv','MR.LteScPHR','MR.LteScAOA','MR.LteScSinrUL','MR.LteScRI','MR.CQI','MR.LteScPUSCHPRBNum','MR.LteScPDSCHPRBNum','MR.LteScBSR','MR.CDMAtype','MR.CDMANcBand','MR.CDMANcArfcn','MR.CDMAPNphase','MR.LteCDMAorHRPDNcPilotStrength','MR.CDMANcPci','MR.Longitude','MR.Latitude','MR.pRRUID'])
df1=df1[['MR.LteScRSRP','MR.LteNcRSRP','MR.LteScRSRQ','MR.LteNcRSRQ','MR.LteScEarfcn','MR.LteScPci','MR.LteNcEarfcn','MR.LteNcPci','MR.LteScTadv','MR.LteScPHR','MR.LteScAOA','MR.LteScSinrUL','MR.LteScRI','MR.CQI','MR.LteScPUSCHPRBNum','MR.LteScPDSCHPRBNum','MR.LteScBSR','MR.Longitude','MR.Latitude']]
df=pd.merge(df,df1,right_index=True, left_index=True)  #市电停电表对标合并
df=df[['eNBID','cellid','MR.LteScRSRP','MR.LteNcRSRP','MR.LteScRSRQ','MR.LteNcRSRQ','MR.LteScEarfcn','MR.LteScPci','MR.LteNcEarfcn','MR.LteNcPci','MR.LteScTadv','MR.LteScPHR','MR.LteScAOA','MR.LteScSinrUL','MR.LteScRI','MR.CQI','MR.LteScPUSCHPRBNum','MR.LteScPDSCHPRBNum','MR.LteScBSR','MR.Longitude','MR.Latitude']]
df.to_csv('d:\MRO.CSV')
end = time.clock()
print('Running time: %s Seconds'%(end-start))